package com.imooc.service.Impl;

import com.imooc.dao.ShopMapper;
import com.imooc.dto.ImageHolder;
import com.imooc.dto.ShopExecution;
import com.imooc.entity.Shop;
import com.imooc.enums.ShopStateEnum;
import com.imooc.exceptions.ShopOperationException;
import com.imooc.service.ShopService;
import com.imooc.util.ImageUtil;
import com.imooc.util.PathUtil;
import org.apache.taglibs.standard.lang.jstl.NullLiteral;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @create 2018-07-31 14:22
 **/
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    /**
     * 根据条件返回对应的商店信息
     * @param shop
     * @return
     */
    @Override
    public List<Shop> getShopList(Shop shop) {
        List<Shop> shopList = shopMapper.selectByShopCondition(shop);

        return shopList;
    }

    /**
     * 添加店铺信息
     *
     * @param shop
     * @param shopImgInputStream
     * @return
     */
    @Override
    @Transactional
    public ShopExecution addShop(Shop shop, ImageHolder thumbnail) {
        if (shop == null) {
            //返回店铺为空的信息
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }
        try {
            //店铺信息的初始化
            shop.setEnableStatus(0);
            shop.setCreateTime(new Date());
            shop.setLastEditTime(new Date());
            //插入店铺信息
            int effectedNum = shopMapper.insert(shop);
            //插入数据状态判断
            if (effectedNum <= 0) {
                throw new RuntimeException("店铺创建失败");
            } else {
                if (thumbnail.getImageInputStream() != null) {
                    //存储图片
                    try {
                        addShopImg(shop, thumbnail);

                    } catch (Exception e) {
                        throw new ShopOperationException("addShop error:" + e.getMessage());
                    }
                    //更新店铺的图片地址
                    effectedNum = shopMapper.updateShop(shop);
                    if (effectedNum <= 0) {
                        throw new ShopOperationException("更新图片地址失败");
                    }
                }
            }
        } catch (Exception e) {
            throw new ShopOperationException("addShop error:" + e.getMessage());
        }

        return new ShopExecution(ShopStateEnum.CHECK, shop);
    }

    /**
     * 更具店铺id获取店铺信息
     *
     * @param shopId
     * @return
     */
    @Override
    public Shop getByShopId(int shopId) {
        return shopMapper.queryByShopId(shopId);
    }

    @Override
    public ShopExecution modifyShop(Shop shop, ImageHolder thumbnail) throws ShopOperationException {
        if (shop == null || shop.getShopId() == null) {
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        } else {
            try {
                //1 判断是否需要处理图片
                if (thumbnail.getImageInputStream() != null && thumbnail.getImageName() != null && !"".equals(thumbnail.getImageName())) {
                    Shop tempShop = shopMapper.queryByShopId(shop.getShopId());
                    if (tempShop.getShopImg() != null) {
                        ImageUtil.deleteFileOrPath(tempShop.getShopImg());
                    }
                    addShopImg(shop, thumbnail);
                }
                //2 更新店铺信息
                shop.setLastEditTime(new Date());
                int effectedNum = shopMapper.updateShop(shop);
                if (effectedNum <= 0) {
                    return new ShopExecution(ShopStateEnum.INNER_ERROR);
                } else {
                    shop = shopMapper.queryByShopId(shop.getShopId());
                    return new ShopExecution(ShopStateEnum.SUCCESS, shop);
                }
            } catch (Exception e) {
                throw new ShopOperationException("modifyShop error:" + e.getMessage());
            }
        }
    }


    /**
     * 添加店铺的图片
     *
     * @param shop
     * @param
     */
    private void addShopImg(Shop shop, ImageHolder thumbnail) {
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        //图片的地址
        String shopImgAddr = ImageUtil.generateThumbnail(thumbnail, dest);
        shop.setShopImg(shopImgAddr);

    }
}
